package com.syf.core.service.run;

import cn.hutool.core.util.StrUtil;
import com.syf.core.engine.SyFrameContext;
import com.syf.core.kit.resource.KitResourceReleaseController;
import com.syf.core.service.context.ServiceRequest;
import com.syf.core.service.context.ServiceResponse;
import com.syf.core.service.enums.ResponseFormatEnums;
import com.syf.core.service.invoke.ServiceInvokeCallback;
import com.syf.core.service.run.chain.ServiceExecutionChain;
import lombok.extern.slf4j.Slf4j;

/**
 * 在本线程内执行callback
 */
@Slf4j
public class SyServiceCallback extends SyServiceCallable {
    private ServiceInvokeCallback<ServiceResponse> callback;

    public SyServiceCallback(ServiceRequest request, ServiceExecutionChain executionChain, SyFrameContext frameContext, KitResourceReleaseController kitResourceReleaseController, ServiceInvokeCallback<ServiceResponse> callback) {
        super(request, executionChain, frameContext, kitResourceReleaseController);
        this.callback = callback;
    }

    @Override
    public ServiceResponse call() {
        ServiceResponse response = super.call();

        if (StrUtil.equals(response.getRespCode(), ResponseFormatEnums.SUCCESS.getRespCode())) {
            callback.onSuccess(response);
        } else {
            callback.onFailure(response);
        }

        return null;
    }
}
